package com.topscomm.mis.cbb.pm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.BusinessException;
import com.topscomm.basic.exception.MessageException;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.cbo.pojo.*;
import com.topscomm.cbo.service.*;
import com.topscomm.mis.cbb.pm.base.pojo.*;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskBackRecordService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskConstraintService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskProgressService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskService;
import com.topscomm.mis.cbb.pm.enumerate.TaskStatusEnum;
import com.topscomm.mis.cbb.pm.service.abs.AbstractTaskProgressService;
import com.topscomm.mis.cbb.pm.service.abs.AbstractTaskService;
import com.topscomm.mis.cbb.pm.service.chain.TaskCompleteHandler;
import com.topscomm.mis.cbb.pm.service.chain.handler.CompetitionHandler;
import com.topscomm.mis.cbb.pm.service.chain.handler.CountersignHandler;
import com.topscomm.mis.cbb.pm.service.chain.handler.OtherHandler;
import com.topscomm.mis.cbb.pm.util.FlowNode;
import com.topscomm.mis.cbb.pm.util.PmSystemConst;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.tap.common.TapSystemConst;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TaskProgressService extends AbstractTaskProgressService {

    @Autowired
    private ICbbPmTaskService cbbPmTaskService;
    @Autowired
    private ICbbPmTaskProgressService cbbPmTaskProgressService;
    /**
     * @Description:    确认最新进度后同步更新任务表进度状态
     * @Author:         wuyuyang
     * @Return:         void
     * @exception:
     * @Date:           28/6/2023 上午8:49
     * @Version:        1.0
     **/
    @Override
    protected void afterInsert(Map<String, Object> paramMap) throws ServiceException {
         String taskcode =  ConvertUtil.convertToString(paramMap.get("taskcode"));
         if (StringUtils.isEmpty(taskcode)) {
             throw new MessageException("获取任务编码失败, 请联系管理员！");
         }
         String progress =  ConvertUtil.convertToString(paramMap.get("progress"));
         if (StringUtils.isEmpty(progress)) {
             throw new MessageException("获取任务进度失败, 请联系管理员！");
         }
         // 更新任务进度
         StringBufferProxy sql = new StringBufferProxy();
         sql.appendSingle("code = '{0}'", taskcode);
         CbbPmTaskEntity cbbPmTaskEntity = cbbPmTaskService.queryObjectByWhere(sql.toString()+" and currently="+ PmSystemConst.CboYesNo.YES);
         cbbPmTaskEntity.setProgress(ConvertUtil.convertToInt(progress));

    }
    /**
     * @Description:    重写insert方法
     * @Author:         wuyuyang
     * @Return:         java.util.Map<java.lang.String,java.lang.Object>
     * @exception:
     * @Date:           28/6/2023 上午9:44
     * @Version:        1.0
     **/
    @Override
    @Transactional
    public Map<String, Object> insert(Map<String, Object> paramMap) {
        beforeInsert(paramMap);
        CbbPmTaskProgressEntity cbbPmTaskProgressEntity = new CbbPmTaskProgressEntity();
        cbbPmTaskProgressEntity.convertFromMap(paramMap);
        cbbPmTaskProgressEntity = cbbPmTaskProgressService.insert(cbbPmTaskProgressEntity);
        Map<String, Object> dataMap = cbbPmTaskProgressEntity.convertToMap();
        afterInsert(dataMap);
        // 确认最新进度后同步更新任务表进度状态
        String taskcode =  ConvertUtil.convertToString(paramMap.get("taskcode"));
        if (StringUtils.isEmpty(taskcode)) {
            throw new MessageException("获取任务编码失败, 请联系管理员！");
        }
        String progress =  ConvertUtil.convertToString(paramMap.get("progress"));
        if (StringUtils.isEmpty(progress)) {
            throw new MessageException("获取任务进度失败, 请联系管理员！");
        }
        // 更新任务进度
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("code = '{0}'", taskcode);
        CbbPmTaskEntity cbbPmTaskEntity = cbbPmTaskService.queryObjectByWhere(sql.toString()+" and currently="+ PmSystemConst.CboYesNo.YES);
        cbbPmTaskEntity.setProgress(ConvertUtil.convertToInt(progress));
        cbbPmTaskService.updateEntity(cbbPmTaskEntity);
        return dataMap;
    }

}
